#include "TcpServer.hpp"
#include "Http.hpp"
void Usage(std::string proc)
{
    std::cout << "Usage:\n\t" << proc << "local_port\n"
              << std::endl;
}

std::shared_ptr<HttpResponse> Login(std::shared_ptr<HttpRequest> req)
{
    LOG(DEBUG, "=========================\n");
    // 自由处理参数了
    std::string userdata;
    if (req->Method() == "GET")
    {
        userdata = req->Args();
    }
    else if (req->Method() == "POST")
    {
        userdata = req->Text();
    }
    else
    {
    }
    // 1. 进程间通信， 比如 pipe、环境变量
    // 2. fork();
    // 3. exec(); python / php / java / C++

    // 处理数据了
    LOG(DEBUG, "enter data handler, data is : %s\n", userdata.c_str());

    auto response = Factory::BuildHttpResponse();
    response->AddStatusLine(200, "OK");
    response->AddHander("Content_Type", "text/html");
    response->AddText("<html><h1>handler data done,login success</h1></html>");
    LOG(DEBUG, "=========================\n");
    return response;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[1]);
        return 1;
    }
    EnableScreen();
    uint16_t port = std::stoi(argv[1]);
    HttpServer http_s;
    // 仿照路径来进行功能路由
    http_s.AddHandler("/login", Login);
    // http_s.AddHandler("路径",功能);
    // http_s.AddHandler("路径",功能);
    // http_s.AddHandler("路径",功能);
    TcpServer tcp_s(port, std::bind(&HttpServer::HandlerHttpRequest, &http_s, std::placeholders::_1));
    tcp_s.Loop();
    return 0;
}